package com.zl.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.zl.common.Constant;
import com.zl.common.enumType.Gglb;
import com.zl.entity.BrowseRecord;
import com.zl.entity.City;
import com.zl.entity.Collect;
import com.zl.entity.Details;
import com.zl.entity.NewsEntity;
import com.zl.entity.Province;
import com.zl.entity.QueryEntity;
import com.zl.service.BorwseService;
import com.zl.service.CollectService;
import com.zl.service.NewsService;
import com.zl.service.SelectorService;
import com.zl.util.SearchUtils;
import com.zl.weixin.entity.UserClient;


/** 
* @ClassName: BrowseController 
* @Description: 最新浏览
* @author chxl
* @date 2015-4-30 下午02:26:47 
*  
*/
@Controller
@RequestMapping("/browse")
@Scope("prototype")
public class BrowseController {
	@Autowired
	private BorwseService borwseService;
	@Autowired
	private NewsService newsService;
	@Autowired
	private SelectorService selectorService;
	@Autowired
	private CollectService collectService;
	@RequestMapping("/index")
	public String list(Model model,HttpServletRequest request){
		UserClient user = (UserClient)request.getSession().getAttribute("user");
//		UserClient user = new UserClient();
//		user.setOpenid("oFOgks6AbGnBns_u2teOtiMG711c");
		List<BrowseRecord> list = borwseService.getBrowseByUser(user.getOpenid());
		List<NewsEntity> browselList = new ArrayList<NewsEntity>();
		for(BrowseRecord record:list){
//			Details details = new Details();
//			details.setTableName(record.getCategory());
//			details.setId(record.getNewsId());// 查询类
//			
//			Details detail = newsService.selectDetailById(details);
//			detail.setTableName(record.getCategory());
//			String columnId = detail.getColumnId();
//			List<City> cityList = selectorService.getCityByColumnId(columnId);
//			if(cityList.size()>0){
//				City city = cityList.get(0);
//				String areaName = city.getName();
//				if(Constant.PROVINCE_GRADE.equals(city.getName())){
//					Province province = selectorService.getProvinceById(city.getParentId());
//					areaName = province.getName();
//				}
//				detail.setCity(areaName);
//				
//			}
//			detail.setCategory(Gglb.getGglb(record.getCategory()).getName());
//			List<Collect> collectList = collectService.isExist(user.getOpenid(), record.getNewsId());
//			detailList.add(detail);
			QueryEntity queryEntity = new QueryEntity();
			queryEntity.setId(record.getNewsId());
			List<NewsEntity> newslist = SearchUtils.search(queryEntity);
			if(newslist.size()>0){
				NewsEntity news = newslist.get(0);
				List<Collect> collectList = collectService.isExist(user.getOpenid(), record.getNewsId());
				if(collectList!=null&&collectList.size()>0){
					news.setCollect(true);
				}
				//System.out.println(news.isCollect());
				browselList.add(news);
			}
		}
		model.addAttribute("browselList", browselList);
		return "uc_br";
	}
}
